<template>
  <view class="detail-item">
    <!-- 动态渲染头部字段 -->
    <view class="detail-header">
      <view class="detail-title">
        <text
          v-for="(headerField, index) in config.headerFields"
          :key="headerField.key"
          :class="index === 0 ? 'primary-text' : 'secondary-text'"
        >
          {{ getFieldValue(headerField, item) }}
        </text>
      </view>
    </view>

    <!-- 动态渲染详情字段 -->
    <view class="detail-details">
      <template v-for="field in getVisibleDetailFields(item)" :key="field.key">
        <!-- 普通字段 -->
        <view v-if="field.type !== 'file'" class="detail-row">
          <text class="detail-label">{{ field.label }}：</text>
          <text class="detail-value">{{ getFieldValue(field, item) }}</text>
        </view>

        <!-- 文件字段 -->
        <view v-else class="detail-row">
          <text class="detail-label">{{ field.label }}：</text>
          <view class="file-attachments">
            <slot name="file-display" :field="field" :item="item">
              <!-- 默认文件展示 -->
              <view class="file-attachment">
                <uni-icons type="paperplane" size="16" color="#059669" />
                <text class="file-name">{{
                  field.fileConfig?.fileNamePrefix || "文件"
                }}</text>
                <text class="preview-hint">点击预览</text>
              </view>
            </slot>
          </view>
        </view>
      </template>
    </view>

    <!-- 操作按钮区域 -->
    <view v-if="showActions" class="detail-actions">
      <slot name="actions" :item="item">
        <!-- 默认删除按钮 -->
        <button class="delete-button" @click="$emit('delete', item)">
          删除
        </button>
      </slot>
    </view>
  </view>
</template>

<script setup lang="ts">
import type {
  DetailDisplayConfig,
  DetailFieldConfig,
} from "./detailDisplayConfig";

// Props
interface Props {
  config: DetailDisplayConfig;
  item: any;
  showActions?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  showActions: true,
});

// Emits
defineEmits<{
  delete: [item: any];
}>();

// 配置驱动的字段值获取方法
const getFieldValue = (field: DetailFieldConfig, item: any): string => {
  if (field.formatter) {
    return field.formatter(item[field.key], item);
  }
  return item[field.key] || field.defaultValue || "-";
};

// 获取可见的详情字段
const getVisibleDetailFields = (item: any): DetailFieldConfig[] => {
  return props.config.detailFields.filter((field) => {
    if (field.visible) {
      return field.visible(item);
    }
    return true;
  });
};
</script>

<style scoped>
.detail-item {
  background: #ffffff;
  border-radius: 24rpx;
  padding: 32rpx 32rpx 16rpx 32rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
  border: 2rpx solid #f0f0f0;
  position: relative;
}

.detail-header {
  margin-bottom: 24rpx;
}

.detail-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 16rpx;
}

.primary-text {
  font-size: 32rpx;
  font-weight: 600;
  color: #1f2937;
  flex: 1;
}

.secondary-text {
  font-size: 28rpx;
  color: #6b7280;
  font-weight: 500;
}

.detail-details {
  display: flex;
  flex-direction: column;
  gap: 4rpx;
}

.detail-row {
  display: flex;
  align-items: flex-start;
  min-height: 48rpx;
}

.detail-label {
  font-size: 28rpx;
  color: #6b7280;
  font-weight: 500;
  min-width: 160rpx;
  flex-shrink: 0;
  margin-right: 16rpx;
}

.detail-value {
  font-size: 28rpx;
  color: #374151;
  flex: 1;
  word-break: break-all;
  line-height: 1.4;
}

.file-attachments {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
  flex: 1;
}

.file-attachment {
  display: flex;
  align-items: center;
  gap: 16rpx;
  padding: 16rpx 24rpx;
  background: #f8fafc;
  border-radius: 16rpx;
  border: 2rpx solid #e2e8f0;
  cursor: pointer;
  transition: all 0.2s ease;
}

.file-attachment:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
}

.file-name {
  font-size: 28rpx;
  color: #059669;
  font-weight: 500;
  flex: 1;
}

.preview-hint {
  font-size: 24rpx;
  color: #6b7280;
}

.detail-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 24rpx;
  padding-top: 16rpx;
  border-top: 2rpx solid #f3f4f6;
}

.delete-button {
  background: #ef4444;
  color: #ffffff;
  border: none;
  border-radius: 12rpx;
  padding: 0rpx 24rpx;
  font-size: 24rpx;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.delete-button:hover {
  background: #dc2626;
}

.delete-button:active {
  background: #b91c1c;
}
</style>
